import Mock from 'mockjs'
import '@/mock/extend'
import {parseUrlParams} from '@/utils/request'
const Random = Mock.Random;

const source = Mock.mock({
  'list|100': [{
    'warningTime': () => Random.datetime('yyyy-MM-dd HH:mm:ss'), // 随机生成预警时间
    'warningItem|1': ['温度', '湿度', '压力', '流量'], // 随机选择预警项
    'warningSign|1': ['警告', '严重', '一般'], // 随机选择预警标志
    'warningValue|0-100.2': 0, // 随机生成预警值，保留两位小数
  }]
});

Mock.mock(RegExp(`${process.env.VUE_APP_API_BASE_URL}/warningList` + '.*'),'get', ({url}) => {
  const params = parseUrlParams(decodeURI(url))
  let {page, pageSize} = params
  page = eval(page) - 1 || 0
  pageSize = eval(pageSize) || 10

  delete params.page
  delete params.pageSize

  let result = source.list.filter(item => {
    for (let [key, value] of Object.entries(params)) {
      if (item[key] !== value) {
        return false
      }
    }
    return true
  })
  const total = result.length
  if ((page) * pageSize > total) {
    result = []
  } else {
    result = result.slice(page * pageSize, (page + 1) * pageSize)
  }

  return {
    code: 0,
    message: 'success',
    data: {
      page: page + 1,
      pageSize,
      total: 100,
      list: result
    }
  }
})
